import Mock from "mockjs";

// import { ref, reactive } from "vue";
// var data = reactive({});
var data = {};
var address = {};
var tokendata = {};
if (localStorage.getItem("user_info")) {
    data = JSON.parse(localStorage.getItem("user_info"));
} else {
    // 生成数据
    data = Mock.mock({
        "list|50": [
            {
                "id": '@increment()',
                'phone|13000000000-19000000000': 1,
                'password|100000-1000000000': 1,
            }
        ]
    })
    localStorage.setItem("user_info", JSON.stringify(data));
}
if (localStorage.getItem("user_address")) {
    address = JSON.parse(localStorage.getItem("user_address"));
} else {
    // 生成数据
    address = Mock.mock({
        "list|50": [
            {
                "id": "@id",
                "name": "@cname",
                "age|20-30": 0,
                "date": '@date("yyyy-MM-dd")',
                "phone|13000000000-19000000000": 1,
                "email": "@email",
                "addresslist": "@county(true)",
                'phone|13000000000-19000000000': 1
            }
        ]
    })
    localStorage.setItem("user_address", JSON.stringify(address));
}

// Mock.mock('/api/user/info')
Mock.mock('/api/user/login', "post", (req) => {
    var { phone, pass } = JSON.parse(req.body);
    var index = data.list.findIndex((item) => {
        return phone == item.phone;
    });

    console.log(index);
    if (index != -1) {
        if (data.list[index].password == pass) {
            console.log(index);
            tokendata = Mock.mock({
                'tokenarr': [
                    {
                        token: "@string(32)"
                    }
                ]
            });
            return { code: 200, msg: "登陆成功", token: tokendata.tokenarr[0].token };
        } else {
            return { code: 500, msg: "用户名或密码错误" };
        }
    } else {
        return { code: 500, msg: "用户名或密码错误" };
    }
});
Mock.mock('/api/user/register', "post", (req) => {
    var { phone, pass } = JSON.parse(req.body);
    // console.log(phone, pass);
    // console.log(data.list);
    var index = data.list.findIndex((item) => {
        return phone == item.phone;
    });
    // console.log(index);
    if (index == -1) {
        // console.log(index);
        data.list.push({ id: data.list.length + 1, phone, password: pass, });
        localStorage.setItem("user_info", JSON.stringify(data));
        phone = "";
        pass = "";
        return { code: 200, msg: "注册成功", list: data.list };

    } else {
        return { code: 500, msg: "该账号已被注册", list: data.list };
    }
})
Mock.mock('/api/user/change', "post", (req) => {
    var { phone, pass } = JSON.parse(req.body);
    console.log(phone);
    console.log(pass);
    var index = data.list.findIndex((item) => {
        return phone == item.phone;
    });
    if (index != -1) {
        console.log(index);
        delete data.list[index].password;
        console.log(data.list[index]);
        data.list[index].password = pass;
        console.log(data.list[index]);
        localStorage.setItem("user_info", JSON.stringify(data));
        return { code: 200, msg: "修改密码成功", list: data.list };
    } else {
        return { code: 500, msg: "修改密码失败", list: data.list };
    }
});
Mock.mock('/api/address/list', "get", () => {
    return address;
});
Mock.mock('/api/address/del', "post", (req) => {
    let addressId = JSON.parse(req.body);
    console.log(addressId)
    var addressIndex = address.list.findIndex((item) => {
        return addressId.id == item.id;
    })
    // console.log(addressIndex)
    if (addressIndex != -1) {
        address.list.splice(addressIndex, 1);
        localStorage.setItem("user_address", JSON.stringify(address));
        return { code: 200, msg: "删除地址成功" }
    } else {
        return { code: 400, msg: "删除地址失败" }
    }

});
Mock.mock('/api/address/search', "post", (req) => {
    console.log(req.body);
    let Id = JSON.parse(req.body);
    console.log(Id);
    var index = address.list.findIndex((item) => {
        return item.phone == Id.id
    });
    var name = address.list.findIndex((item) => {
        return item.name == Id.id
    });
    var arr = index != -1 ? address.list.slice(index, index + 1) : (address.list.slice(name, name + 1));
    // console.log(index);
    if (arr.length) {
        console.log(arr);
        return { code: 200, msg: "查询用户成功", list: arr }
    } else {
        return { code: 400, msg: "没有符合的用户" }
    }
});
Mock.mock('/api/address/edit', 'post', (req) => {
    let request = JSON.parse(req.body).query;
    // let request = JSON.parse(req.body);
    let query = JSON.parse(request);
    // console.log(query);
    var index = address.list.findIndex((item) => {
        return item.id == query.id;
    });
    // console.log(index);
    if (index != -1) {
        address.list.splice(index, 1, query);
        console.log(address.list);
        localStorage.setItem('user_address', JSON.stringify(address));
        return { code: 200, msg: "编辑成功" };
    } else {
        return { code: 400, msg: "编辑失败" };
    }

})
